package org.gikolet.ch2.android.widget;

import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.gikolet.ch2.data.Board;
import org.gikolet.ch2.data.ThreadHeader;
import org.gikolet.ch2.logic.ThreadHeadersLogic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.inject.Inject;
import com.google.inject.name.Named;

public class ThreadHeaderAdapter extends BaseAdapter {

	public static final String SUBJECTS_LAYOUT_ID = "subjectsLayoutId";
	public static final String SUBJECT_DATE_VIEW_ID = "subjectDateViewId";
	public static final String SUBJECT_RES_COUNT_VIEW_ID = "subjectResCountViewId";
	public static final String SUBJECT_TEXT_VIEW_ID = "subjectTextViewId";

	@Inject
	private Context context;

	@Inject
	@Named(SUBJECTS_LAYOUT_ID)
	private int layout;

	@Inject
	private ThreadHeadersLogic subjectsLogic;

	@Inject
	@Named(SUBJECT_DATE_VIEW_ID)
	private int subjectDateViewId;

	@Inject
	@Named(SUBJECT_RES_COUNT_VIEW_ID)
	private int subjectResCountViewId;

	@Inject
	@Named(SUBJECT_TEXT_VIEW_ID)
	private int subjectTextViewId;

	private Board board;

	private List<ThreadHeader> subjectCache = new ArrayList<ThreadHeader>();

	private static final DateFormat GREGORIAN_FORMAT = DateFormat
			.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM);

	public ThreadHeaderAdapter() {
	}

	private List<ThreadHeader> getSubjectCache() {
		if (this.board == null) {
			this.subjectCache.clear();

		} else if (this.subjectCache.isEmpty()) {
			try {
				this.subjectCache.addAll(this.subjectsLogic
						.getThreadHeaders(this.board));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return this.subjectCache;
	}

	public int getCount() {
		return getSubjectCache().size();
	}

	public ThreadHeader getItem(int i) {
		return this.getSubjectCache().get(i);
	}

	public long getItemId(int i) {
		return i;
	}

	public void setBoard(Board board) {
		this.board = new Board(board);

		this.subjectCache.clear();
		this.notifyDataSetChanged();
	}

	public Board getBoard() {
		return new Board(this.board);
	}

	public View getView(int i, View convertView, ViewGroup viewgroup) {
		View view;

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) this.context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(this.layout, viewgroup, false);
		} else {
			view = convertView;
		}

		TextView dateTextView = (TextView) view
				.findViewById(this.subjectDateViewId);
		TextView resCountTextView = (TextView) view
				.findViewById(this.subjectResCountViewId);
		TextView subjectTextView = (TextView) view
				.findViewById(this.subjectTextViewId);

		ThreadHeader threadHeader = (ThreadHeader) getItem(i);

		dateTextView.setText(getDateString(threadHeader.getDate()));

		resCountTextView.setText("(" + threadHeader.getResCount() + ")");
		subjectTextView.setText(threadHeader.getSubject());

		return view;
	}

	private String getDateString(Date date) {
		if (date == null) {
			return null;
		}
		// Android遅い…
		// キャッシュするべきか？
		return GREGORIAN_FORMAT.format(date);
	}
}
